var truckId;
$(function () {
    truckId = $.getUrlParam("id");
    //显示司机的下拉列表
    $.ajax({
        url: "../driver/getDriverIdAndRealName.htm",
        cache: false,
        async:false,
        type: "get",
        dataType: "json",
        success: function (result) {
            if (result.status == 0) {
                var data = result.data;
                addDriverOptions(data, "#truckDriver");
                return;
            }
        }
    });
    
    //显示分站点的下拉列表
    $.ajax({
        url: "../company/getCompanyNumAndNameUsedSelect.htm",
        cache: false,
        async:false,
        type: "get",
        dataType: "json",
        success: function (result) {
            if (result.status == 0) {
                var data = result.data;
                addCompanyOptions(data, "#truckCompany");
                return;
            }
        }
    });
    
    //是否验证成功
    $('#editTruckForm').validationEngine('attach', {
        validationEventTrigger : 'blur',
        promptPosition: 'inline',
        scroll: false,
        focusFirstField : true,
        onValidationComplete : function (form, valid) {
            if (valid){
                if (truckId == 'null') {
                	addtruckInfo();
                } else {
                	modifytruckInfo();
                }
            }
        }
    });
    
    //如果truckId为null，则是增加操作，否则是编辑操作.
    //这个地方使用的return，后面的语句将执行不到
    if(truckId == 'null'){
    	return;
    }else{
    	echotruckInfo();
    }
});

//点击编辑回显数据
function echotruckInfo(){
    $.ajax({
        url: "../truck/getTruckInfoUsedEdit.htm",
        cache: false,
        type: "get",
        dataType: "json",
	    data: {"truckId": truckId},
        success: function (result) {
            if (result.status == 0) {
                var data = result.data;
                $("#editTruckForm").formatForm(data);
                return;
            }
        }
    });
}

//关闭窗口
function closeForm() {
    var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
    parent.layer.close(index);
}

//修改货车信息
function modifytruckInfo(){
    $.ajax({
        url: "../truck/modifyTruckInfo.htm",
        cache: false,
        type: "post",
        dataType: "json",
        data : $("#editTruckForm").serialize(),
        beforeSend:function(){var index = layer.load(1, { shade: [0.8,'#ddd']});},
        complete:function(){var index = layer.closeAll("loading");},
        success: function (result) {
            if (result.status == 0) {
                parent.layer.msg(result.message, {icon: 1});
                parent.$table.bootstrapTable('refresh', {url: '../truck/getTruckInfoByPage.htm'});
            } else {
                parent.layer.msg(result.message, {icon: 2});
            }
            closeForm();
        }
    });
}

//增加货车信息
function addtruckInfo() {
    $.ajax({
        url: "../truck/addToTruckInfo.htm",
        cache: false,
        type: "post",
        dataType: "json",
        data: $("#editTruckForm").serialize(),
        beforeSend:function(){var index = layer.load(1, { shade: [0.8,'#ddd']});},
        complete:function(){var index = layer.closeAll("loading");},
        success: function (result) {
            if (result.status == 0) {
                parent.layer.msg(result.message, {icon: 1});
                parent.$table.bootstrapTable('refresh', {url: '../truck/getTruckInfoByPage.htm'});
                closeForm();
            } else {
                parent.layer.msg(result.message, {icon: 2});
                closeForm();
            }
        }
    });
}

//司机下拉列表  追加到option中
function addDriverOptions(data, selectId){
	for(var i=0; i<data.length; i++){
		$(selectId).append('<option value="' + data[i].driverId + '">' + data[i].driverRealName + '</option>');
	}
}

//公司下拉列表  追加到option中
function addCompanyOptions(data, selectId){
	for(var i=0; i<data.length; i++){
		$(selectId).append('<option value="' + data[i].comnumId + '">' + data[i].comnumName + '</option>');
	}
}